/**
 * 工具类
 */
// AES加密
import CryptoJS from 'crypto-js'

import moment from 'moment'

import store from 'store2'

import { rotateBase64Img } from '../utils/utils'

// 十六位十六进制数作为密钥
const key = CryptoJS.enc.Utf8.parse('y4s60Xmw0Z2ucm85')
// 十六位十六进制数作为密钥偏移量
const iv = CryptoJS.enc.Utf8.parse('e4X4Uc9130V8450L')

/**
 * 登录密码 用的加密方法
 * @return {string}
 */
export function AesEncrypt (word) {
  let srcs = CryptoJS.enc.Utf8.parse(word)
  let encrypted = CryptoJS.AES.encrypt(srcs, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  })
  return encrypted.toString()
}

export function pictureEditor (data, deg) {
  return new Promise((resolve, reject) => {
    rotateBase64Img(data, deg, e => {
      resolve(e)
    })
  })
}

/**
 * 解密方法
 * @return {string}
 */
export function AesDecrypt (word) {
  let encryptedHexStr = CryptoJS.enc.Hex.parse(word)
  let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr)
  let decrypt = CryptoJS.AES.decrypt(srcs, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  })
  let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8)
  return decryptedStr.toString()
}
/**
 * 获取用户的token
 * @return {string}
 */
export function getStorage (key) {
  let token
  if (sessionStorage.getItem(key)) {
    token = sessionStorage.getItem(key)
  } else if (store.get(key)) {
    token = store.get(key)
  } else {
    token = null
  }
  return token
}
export function offlineFormatToOnlinFormat (data, roomId) {
  const _data = {
    subType: data.type,
    data: {
      // createTime: moment(data.timestamp * 1).format('YYYY-MM-DD HH:mm:ss'),
      msg: data.msg,
      roomId: roomId
    }
  }
  if (data.timestamp) {
    _data.data.createTime = moment(data.timestamp * 1).format(
      'YYYY-MM-DD HH:mm:ss'
    )
  } else {
    _data.data.createTime = moment().format('YYYY-MM-DD HH:mm:ss')
  }
  if (data.sourceId) {
    _data.data.sourceId = data.sourceId
  }
  if (data.fileName) {
    _data.data.fileName = data.fileName
  }
  if (data.fileSize) {
    _data.data.fileSize = data.fileSize
  }
  if (data.sender) {
    _data.data.sender = data.sender
  }
  if (data.tone) {
    _data.data.tone = data.tone
  }
  return _data
}

/**
 * @description 格式化金额
 * @param number：要格式化的数字
 * @param decimals：保留几位小数 默认0位
 * @param decPoint：小数点符号 默认.
 * @param thousandsSep：千分位符号 默认为,
 */
export function formatMoney(number, decimals = 0, decPoint = '.', thousandsSep = ','){
  number = (number + '').replace(/[^0-9+-Ee.]/g, '')
  let n = !isFinite(+number) ? 0 : +number
  let prec = !isFinite(+decimals) ? 0 : Math.abs(decimals)
  let sep = (typeof thousandsSep === 'undefined') ? ',' : thousandsSep
  let dec = (typeof decPoint === 'undefined') ? '.' : decPoint
  let s = ''
  let toFixedFix = function (n, prec) {
    let k = Math.pow(10, prec)
    return '' + Math.ceil(n * k) / k
  }
  s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.')
  let re = /(-?\d+)(\d{3})/
  while (re.test(s[0])) {
    s[0] = s[0].replace(re, '$1' + sep + '$2')
  }
  if ((s[1] || '').length < prec) {
    s[1] = s[1] || ''
    s[1] += new Array(prec - s[1].length + 1).join('0')
  }
  return s.join(dec)
}
